<?
/*
  Plugin Name: Filtros
  Version: 1.5
  Plugin URI: http://www.ricardolanes.com.br
  Description:
  Author: Ricardo Lanes
  Author URI: http://www.ricardolanes.com.br
 */

function filters_function_init() {
    global $wpdb;


    if ($_POST['filters']) {
        extract($_POST);

        $value = ($value) ? "'".$value."'" : "NULL";
        $parent = ($parent) ? "'".$parent."'" : "NULL";
        $wpdb->query("INSERT INTO wp_filters (label, value, parent) VALUES('$label', $value, $parent)");
    }
    if ($_GET['excluir']) {
        //$wpdb->query("INSERT INTO wp_filters (label, value, parent) VALUES('$label', $value, $parent)");
    }

    $filtros = $wpdb->get_results("SELECT * FROM wp_filters $where ORDER BY parent");
    $parents = $wpdb->get_results("SELECT * FROM wp_filters where parent IS NULL ORDER BY label");
    ?>



    <div class="wrap">
        <div id="icon-edit" class="icon32 icon32-posts-post">
            <br>
        </div>
        <h2>Cadastrar Filtro</h2>

        <p>
        <form id="" action="?post_type=produto&page=loja_filtros" method="POST">

            <input type="text" name="label" placeholder="Nome">
            <input type="text" name="value" placeholder="Valor">

            <select name="parent">
                <option value="">-- Selecione o filtro pai --</option>
                <?
                foreach ($parents as $parent) {
                    ?>
                    <option value="<?= $parent->id_filter; ?>"><?= $parent->label; ?></option>
                    <?
                }
                ?>
            </select>

            <input type="submit" name="filters" class="button button-primary button-large" value="Salvar">

        </form>			
    </p>



    <h2>Lista de Filtros</h2>
    <ul class="subsubsub">
        <li class="all">Total <span class="count">(<?= count($filtros); ?>)</span></li>
    </ul>
    <table class="wp-list-table widefat fixed posts" cellspacing="0">
        <thead>
            <tr>
                <th scope="col" id="author" class="manage-column column-author" style="">Nome</th>
                <th scope="col" id="categories" class="manage-column column-categories" style="">Valor</th>
            </tr>
        </thead>

        <tfoot>
            <tr>
                <th scope="col" id="author" class="manage-column column-author" style="">Nome</th>
                <th scope="col" id="categories" class="manage-column column-categories" style="">Valor</th>
            </tr>
        </tfoot>


        <tbody id="the-list">
            <?
            foreach ($parents as $parent) {
                ?>
                <tr id="post-106" class="post-106 type-post status-publish format-standard hentry category-energetico alternate iedit author-other" valign="top">
                    <td class="post-title page-title column-title">
                        <strong style="color:#202020">
                            <?
                            echo $parent->label;
                            ?>
                        </strong>
                    </td> 
                    <td class="categories column-categories">
                        <?= $parent->value; ?>
                    </td>
                </tr>
                <?
                $filtros = $wpdb->get_results("SELECT * FROM wp_filters where parent = '$parent->id_filter' ORDER BY label");
                foreach ($filtros as $filtro) {
                    ?>

                    <tr id="post-106" class="post-106 type-post status-publish format-standard hentry category-energetico alternate iedit author-other" valign="top">
                        <td class="post-title page-title column-title">
                            <strong style="color:#202020">
                                <?
                                if ($filtro->parent) {
                                    echo " - - ";
                                }

                                echo $filtro->label;
                                ?>
                            </strong>
                            <!--<div class="row-actions">
                                                     <span class="edit">
                                                            <a href="javascript:void(0);confirm('Deseja realmente excluir esse filtro?');" title="Excluir filtro">Excluir</a>
                                                        </span>
                                                    </div>-->
                        </td> 
                        <td class="categories column-categories">
                            <?= $filtro->value; ?>
                        </td>
                    </tr>
                    <?
                }
            }
            ?>

        </tbody>
    </table>
    </div>

    <?
}